#!/bin/sh

export CKMS_APK_DEFER_INITRAMFS=1

for kern in /usr/lib/modules/*; do
    [ -d "${kern}" ] || continue
    kernver=${kern#/usr/lib/modules/}
    # possibly not a kernel, or at least not modular
    [ -f "${kern}/modules.dep" ] || continue
    # skip early
    if [ ! -d "${kern}/build" ]; then
        echo "kernel headers not installed for ${kernver}, skipping..."
        continue
    fi
    ckms -q -k "${kernver}" plain-status | \
        while read modn modv kernv karch status; do
            # only added; build it
            if [ "$status" = "added" ]; then
                ckms -k "${kernv}" build "${modn}=${modv}"
                if [ $? -ne 0 ]; then
                    echo "FAILED: build ${modn}=${modv} for ${kernv}"
                    continue
                fi
                status="built"
            fi
            # only built; install it
            if [ "$status" = "built" ]; then
                ckms -k "${kernv}" install "${modn}=${modv}"
                if [ $? -ne 0 ]; then
                    echo "FAILED: install ${modn}=${modv} for ${kernv}"
                    continue
                fi
            fi
        done
done

for f in /boot/.ckms-initramfs-defer.*; do
    [ -f "$f" ] || continue
    kernver=$(basename $f)
    kernver=${kernver#.ckms-initramfs-defer.}
    update-initramfs -u -k "${kernver}" || \
        echo "FAILED: update-initramfs for ${kernver}"
    rm -f "$f"
done

exit 0
